package com.example.yingjiguanli_demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yingjiguanli_demo.aspect.interfaces.RequirePermission;
import com.example.yingjiguanli_demo.pojo.*;
import com.example.yingjiguanli_demo.service.IApplicationsService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生申请导师表 前端控制器
 * </p>
 *
 * @author scj
 * @since 2024-12-07
 */
@RestController
@RequestMapping("/api/v1/applications")
public class ApplicationsController {
    @Autowired
    private IApplicationsService applicationsService;

    /**
     * 提交申请
     * @param sessionId sessionId
     * @return Result<T>
     */
    @PostMapping("/submit")
    @RequirePermission("user")
    @ApiOperation(value = "提交申请")
    public Result<T> submitApplication(@CookieValue(name = "SESSIONID", required = false) String sessionId,
                                       @RequestBody ApplicationRequest applicationRequest) {
        return applicationsService.submitApplication(sessionId,applicationRequest);
    }

    /**
     * 更新申请状态
     * @param applicationId applicationId
     * @param status 状态
     * @return Result<T>
     */
    @PutMapping("/updateStatus/{applicationId}")
    @RequirePermission("teacher")
    @ApiOperation(value = "更新申请状态")
    public Result<T> updateApplicationStatus(@PathVariable("applicationId") Integer applicationId,
                                                          @RequestParam("status") String status) {
            return applicationsService.updateApplicationStatus(applicationId, status);
    }

    /**
     * 根据sessionId查询申请
     * @param sessionId  sessionId
     * @return  Result<List<Applications>>
     */
    @GetMapping("/getByUserId")
    @RequirePermission("user")
    @ApiOperation(value = "学生查询申请")
    public Result<List<ApplicationWithMentorDTO>> getApplicationBySessionId(@CookieValue(name = "SESSIONID", required = false) String sessionId) {
        return applicationsService.getApplicationBySessionId(sessionId);
    }

    /**
     * 根据id查询申请
     * @param applicationId applicationId
     * @return ResponseEntity<Applications>
     */
    @GetMapping("/get/{applicationId}")
    @RequirePermission("user")
    @ApiOperation(value = "查询申请")
    public ResponseEntity<Applications> getApplication(@PathVariable("applicationId") Integer applicationId) {
        Applications application = applicationsService.getApplication(applicationId);
        if (application != null) {
            return ResponseEntity.ok(application);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }
    /**
     * 分页查询学生申请列表
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @return Result<Page<Applications>>
     */
    @GetMapping("/list")
    @RequirePermission("teacher")
    @ApiOperation(value = "分页查询学生申请列表")
    public Result<Page<ApplicationWithStudentAndMentorDTO>> getApplicationsPage(@RequestParam("pageNum") int pageNum,
                                                                                @RequestParam("pageSize") int pageSize,
                                                                                @CookieValue(name = "SESSIONID", required = false) String sessionId) {

            return applicationsService.getApplicationsPage(sessionId,pageNum, pageSize);
    }
}
